package com.egao.common.publicly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.module.cms.entity.Category;
import com.egao.common.module.cms.service.CategoryService;
import com.egao.common.publicly.entity.CategoryVO;
import com.egao.common.publicly.mapper.PubliclyCategoryMapper;
import com.egao.common.publicly.service.PubliclyCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页栏目接口实现类
 * @author pyx
 */
@Service
public class PubliclyCategoryServiceImpl extends ServiceImpl<PubliclyCategoryMapper, CategoryVO> implements PubliclyCategoryService {
    @Autowired
    private CategoryService categoryService;
    @Override
    public List<CategoryVO> tree() {
        List<CategoryVO> categoryList = baseMapper.selectTree();

        // 构建树形对象
        ArrayList<CategoryVO> tree = new ArrayList<>();

        // 一级树
        for (CategoryVO one : categoryList) {
            if (one.getParentId() == 0) {
                tree.add(one);
            }
        }

        // 二级树
        for (CategoryVO t : tree) {
            ArrayList<CategoryVO> childList = new ArrayList<>();
            for (CategoryVO c : categoryList) {
                if (c.getParentId() != 0 && c.getParentId().equals(t.getId())) {
                    childList.add(c);
                }
            }

            t.setChildren(childList);

            if (t.getChildren().size() == 0) {
                t.setChildren(null);
            }
            //把查询到的子类数据移除
            categoryList.removeAll(childList);
        }

        //把查询到的父类数据移除
        categoryList.removeAll(tree);

        //加入未查到的信息
        tree.addAll(categoryList);

        return tree;
    }

    @Override
    public List<CategoryVO> headCategory() {
        return baseMapper.listHead();
    }

    @Override
    public List<CategoryVO> childrenList() {
        return baseMapper.childrenList();
    }
}
